package dragon.core.io;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import dragon.core.exception.ExceptionHandler;

public class FileManagerImp implements FileManager {

	@Override
	public void copyFile(String source, String target) throws ExceptionHandler {
		FileHandler.copyFile(source, target);
	}

	@Override
	public void createFile(String filePath, String content) throws ExceptionHandler {
		if (isFileExist(filePath)) {
			throw new ExceptionHandler("目标文件已经存在！");
		} else {
			FileHandler.createPlainTextFile(filePath, content, null);
		}
	}

	@Override
	public void createFile(InputStream ins, String filePath) throws ExceptionHandler {
		if (isFileExist(filePath)) {
			throw new ExceptionHandler("目标文件已经存在！");
		} else {
			FileHandler.createBinaryFile(ins, filePath);
		}
	}

	@Override
	public void createFolder(String folderPath) throws ExceptionHandler {
		if (isFileExist(folderPath)) {
			throw new ExceptionHandler("目标文件夹已经存在！");
		} else {
			FileHandler.mkDir(folderPath);
		}
	}

	@Override
	public void deleteFile(String filePath) {
		FileHandler.deleteFiles(filePath, true);
	}

	@Override
	public List<LogicFile> getLogicFiles(String folderPath, int type, boolean recursive, boolean includeSelf) throws ExceptionHandler {
		List<LogicFile> list = FileHandler.getLogicFiles(folderPath, type, recursive);
		if (includeSelf == false) {
			return list;
		} else {
			List<LogicFile> list2 = new ArrayList<LogicFile>();
			LogicFile lf = new LogicFile();
			lf.setPath(folderPath);
			list2.add(lf);
			list2.addAll(list);
			return list2;
		}
	}

	@Override
	public boolean isFileExist(String filePath) throws ExceptionHandler {
		return FileHandler.isFileExists(filePath);
	}

	@Override
	public void modifyFile(String filePath, String content) throws ExceptionHandler {
		if (!isFileExist(filePath)) {
			throw new ExceptionHandler("目标文件不存在！");
		} else {
			FileHandler.createPlainTextFile(filePath, content, null);
		}
	}

	@Override
	public void moveFile(String source, String target) throws ExceptionHandler {
		if (!isFileExist(source)) {
			throw new ExceptionHandler("源文件不存在！");
		}
		if (!isFileExist(target)) {
			throw new ExceptionHandler("目标文件夹不存在！");
		}
		FileHandler.moveFile(source, target);
	}

	@Override
	public void renameFile(String filePath, String newFileName) throws ExceptionHandler {
		if (!isFileExist(filePath)) {
			throw new ExceptionHandler("源文件不存在！");
		}
		FileHandler.renameFile(filePath, newFileName);
	}

	@Override
	public String getFileContent(String filePath) throws ExceptionHandler {
		if (!isFileExist(filePath)) {
			throw new ExceptionHandler("目标文件不存在！");
		}
		return FileHandler.readFile(filePath, false, null);
	}

	@Override
	public List<LogicFile> Path2Tree(List<LogicFile> files) {
		String splitstring = FileHandler.separator + FileHandler.separator;
		int mindepth = 1000;
		int maxdepth = 0;
		int id = 1;
		List<LogicFile> ftl = new ArrayList<LogicFile>();
		List<LogicFile> file = new ArrayList<LogicFile>();
		List<LogicFile> directory = new ArrayList<LogicFile>();
		for (LogicFile f : files) {
			int tem = f.getPath().split(splitstring).length;
			if (tem < mindepth) {
				mindepth = tem;
			}
			if (tem > maxdepth) {
				maxdepth = tem;
			}
			if (f.isFolder()) {
				directory.add(f);
			} else {
				file.add(f);
			}
		}

		for (int i = mindepth; i <= maxdepth; i++) {
			for (LogicFile f : directory) {
				int tem = f.getPath().split(splitstring).length;
				if (tem == i) {
					f.setFileid(id);
					f.setDepth(i);
					++id;
					if (i == mindepth) {
						f.setFilepid(0);
					} else {
						for (LogicFile ftm : ftl) {
							String temString = f.getPath().split(splitstring)[i - 2];
							if (temString.equals(ftm.getName()) && ftm.getDepth() == i - 1) {
								f.setFilepid(ftm.getFileid());
							}
						}
					}
					ftl.add(f);
				}
			}
		}
		for (int i = mindepth + 1; i <= maxdepth; i++) {
			for (LogicFile f : file) {
				int tem = f.getPath().split(splitstring).length;
				if (tem == i) {
					f.setFileid(id);
					f.setDepth(i);
					++id;
					for (LogicFile ftm : ftl) {
						String temString = f.getPath().split(splitstring)[i - 2];
						if (temString.equals(ftm.getName()) && ftm.getDepth() == i - 1) {
							f.setFilepid(ftm.getFileid());
						}
					}
					ftl.add(f);
				}
			}
		}
		return ftl;
	}

}
